package io.xxx.omni.oss.custom

import com.google.gson.JsonObject
import io.xxx.omni.oss.common.format
import io.xxx.omni.oss.common.toJsonString
import io.xxx.omni.oss.common.toLocalDateTime
import io.xxx.omni.oss.domain.Document
import io.xxx.omni.oss.domain.DocumentType

// https://open.aikucun.com/?docType=1&id=53
@Suppress("unused")
class AkcTradeSynchronizer : AkcSynchronizer() {

    override val documentType = DocumentType.TRADE

    override val interfaceName = "aikucun.delivery.trade.order.search"

    override val startTimeLimit = 30u

    override fun getCount(context: Context, response: JsonObject): Int {
        return response["totalRecord"].asInt
    }

    override fun buildDocuments(context: Context, response: JsonObject): List<Document> {
        return response
            .getAsJsonArray("orderList")
            .map {
                it as JsonObject
                Document(
                    it["adOrderId"].asString,
                    it["status"].asString,
                    it.toJsonString(),
                    it["createTime"].toLocalDateTime(),
                    it["updateTime"].toLocalDateTime(),
                )
            }
    }

    override fun buildRequest(context: Context, pageNo: Int): Map<String, Any> {
        return mapOf(
            "startDate" to context.startTime.format(),
            "endDate" to context.endTime.format(),
            "status" to "4",
            "queryType" to "1",
            "pageNo" to pageNo,
            "pageSize" to pageSize,
        )
    }
}